{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>...</th>\n",
       "      <th>12</th>\n",
       "      <th>13</th>\n",
       "      <th>14</th>\n",
       "      <th>15</th>\n",
       "      <th>16</th>\n",
       "      <th>17</th>\n",
       "      <th>18</th>\n",
       "      <th>19</th>\n",
       "      <th>20</th>\n",
       "      <th>21</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>38.5</td>\n",
       "      <td>54</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5.9</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>42.0</td>\n",
       "      <td>6.3</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>37.6</td>\n",
       "      <td>48</td>\n",
       "      <td>36</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>6.3</td>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>37.7</td>\n",
       "      <td>44</td>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>45.0</td>\n",
       "      <td>70.0</td>\n",
       "      <td>3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>37.0</td>\n",
       "      <td>56</td>\n",
       "      <td>24</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>61.0</td>\n",
       "      <td>3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>38.0</td>\n",
       "      <td>42</td>\n",
       "      <td>12</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>37.0</td>\n",
       "      <td>5.8</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   0   1     2   3   4   5   6   7   8   9   ...  12  13   14  15  16    17  \\\n",
       "0   2   1  38.5  54  20   0   1   2   2   3  ...   2   2  5.9   0   2  42.0   \n",
       "1   2   1  37.6  48  36   0   0   1   1   0  ...   0   0  0.0   0   0  44.0   \n",
       "2   1   1  37.7  44  28   0   4   3   2   5  ...   1   1  0.0   3   5  45.0   \n",
       "3   1   1  37.0  56  24   3   1   4   2   4  ...   1   1  0.0   0   0  35.0   \n",
       "4   2   1  38.0  42  12   3   0   3   1   1  ...   0   0  0.0   0   2  37.0   \n",
       "\n",
       "     18  19   20  21  \n",
       "0   6.3   0  0.0   1  \n",
       "1   6.3   1  5.0   1  \n",
       "2  70.0   3  2.0   1  \n",
       "3  61.0   3  2.0   0  \n",
       "4   5.8   0  0.0   1  \n",
       "\n",
       "[5 rows x 22 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train=pd.read_table('horseColicTest.txt',header=None)\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>...</th>\n",
       "      <th>12</th>\n",
       "      <th>13</th>\n",
       "      <th>14</th>\n",
       "      <th>15</th>\n",
       "      <th>16</th>\n",
       "      <th>17</th>\n",
       "      <th>18</th>\n",
       "      <th>19</th>\n",
       "      <th>20</th>\n",
       "      <th>21</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>38.5</td>\n",
       "      <td>66.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>8.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>39.2</td>\n",
       "      <td>88.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>38.3</td>\n",
       "      <td>40.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>6.7</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>39.1</td>\n",
       "      <td>164.0</td>\n",
       "      <td>84.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>7.2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.3</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>37.3</td>\n",
       "      <td>104.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>7.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    0    1     2      3     4    5    6    7    8    9   ...   12   13   14  \\\n",
       "0  2.0  1.0  38.5   66.0  28.0  3.0  3.0  0.0  2.0  5.0  ...  0.0  0.0  0.0   \n",
       "1  1.0  1.0  39.2   88.0  20.0  0.0  0.0  4.0  1.0  3.0  ...  0.0  0.0  0.0   \n",
       "2  2.0  1.0  38.3   40.0  24.0  1.0  1.0  3.0  1.0  3.0  ...  0.0  0.0  0.0   \n",
       "3  1.0  9.0  39.1  164.0  84.0  4.0  1.0  6.0  2.0  2.0  ...  1.0  2.0  5.0   \n",
       "4  2.0  1.0  37.3  104.0  35.0  0.0  0.0  6.0  2.0  0.0  ...  0.0  0.0  0.0   \n",
       "\n",
       "    15   16    17    18   19   20   21  \n",
       "0  3.0  5.0  45.0   8.4  0.0  0.0  0.0  \n",
       "1  4.0  2.0  50.0  85.0  2.0  2.0  0.0  \n",
       "2  1.0  1.0  33.0   6.7  0.0  0.0  1.0  \n",
       "3  3.0  0.0  48.0   7.2  3.0  5.3  0.0  \n",
       "4  0.0  0.0  74.0   7.4  0.0  0.0  0.0  \n",
       "\n",
       "[5 rows x 22 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test =pd.read_table('horseColicTraining.txt',header=None)\n",
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sigmoid(x):\n",
    "    return 1/(1+np.exp(-x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def BGD_LR(df,alpha=0.001,maxCycles=5000):\n",
    "    xMat = np.mat(df.iloc[:,:-1])\n",
    "    yMat = np.mat(df.iloc[:,-1])\n",
    "\n",
    "    m, _ = xMat.shape\n",
    "\n",
    "    xMat = np.column_stack((np.ones(m), xMat))\n",
    "\n",
    "    # 样本的行和列\n",
    "    m,n = xMat.shape\n",
    "    weights = np.zeros((n,1))\n",
    "    # 最优化算法：迭代更新\n",
    "    \n",
    "    for i in range(maxCycles):\n",
    "        h = sigmoid(xMat * weights)\n",
    "        # h = xMat * weights\n",
    "        grad = xMat.T * (h - yMat.T)/m\n",
    "        weights -= alpha*grad\n",
    "    return weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(22, 1)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ws=BGD_LR(train,alpha=0.001,maxCycles=5000)\n",
    "ws.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(299, 22)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xMat=np.array(test.iloc[:,:-1])\n",
    "xMat=np.column_stack((np.ones(xMat.shape[0]),xMat))\n",
    "xMat.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.96270512, 0.39711509, 0.78861886, 0.0399735 , 0.60273371,\n",
       "       0.4059046 , 0.94993145, 0.87166559, 0.38602777, 0.76201958,\n",
       "       0.74649792, 0.92320367, 0.89425245, 0.81751971, 0.8636425 ,\n",
       "       0.28266547, 0.17070784, 0.87148659, 0.92893524, 0.32469606,\n",
       "       0.64813269, 0.97905856, 0.68205296, 0.72754494, 0.80074352,\n",
       "       0.86126615, 0.87252266, 0.95109867, 0.58078868, 0.86525585,\n",
       "       0.85397404, 0.67209188, 0.9590537 , 0.9550417 , 0.19812209,\n",
       "       0.25233274, 0.80262861, 0.87067619, 0.93747013, 0.36532262,\n",
       "       0.09892656, 0.77566493, 0.8363985 , 0.06322769, 0.44888239,\n",
       "       0.18925096, 0.81684909, 0.93729204, 0.81439145, 0.64447227,\n",
       "       0.95163101, 0.47024892, 0.70452202, 0.97383976, 0.96116039,\n",
       "       0.09152138, 0.74665843, 0.15820525, 0.62454597, 0.43019801,\n",
       "       0.92866778, 0.96236501, 0.90338761, 0.3193237 , 0.73734865,\n",
       "       0.09197871, 0.59749359, 0.98057033, 0.48681455, 0.97573414,\n",
       "       0.9519765 , 0.96729235, 0.83188227, 0.5155979 , 0.74076556,\n",
       "       0.20903   , 0.79302134, 0.81101242, 0.75398335, 0.63175714,\n",
       "       0.57850079, 0.9874969 , 0.42682703, 0.56764219, 0.81370691,\n",
       "       0.91931773, 0.30815559, 0.84455495, 0.70472354, 0.87107688,\n",
       "       0.88559278, 0.96116039, 0.96161774, 0.82146353, 0.8805469 ,\n",
       "       0.88034645, 0.52808124, 0.59261691, 0.85618515, 0.64205484,\n",
       "       0.88239525, 0.07659312, 0.79184024, 0.51812962, 0.91607781,\n",
       "       0.48365951, 0.88507076, 0.83541889, 0.82987822, 0.5315313 ,\n",
       "       0.49572294, 0.86588624, 0.65804378, 0.82500027, 0.54883078,\n",
       "       0.42847887, 0.9375617 , 0.70496458, 0.3749549 , 0.80431413,\n",
       "       0.91966992, 0.506962  , 0.80158532, 0.86115963, 0.72326715,\n",
       "       0.94294206, 0.66852706, 0.95026212, 0.30953251, 0.95534129,\n",
       "       0.88036594, 0.77812628, 0.96012427, 0.13319286, 0.13904731,\n",
       "       0.72752159, 0.90680615, 0.75805984, 0.96154636, 0.95371936,\n",
       "       0.80794343, 0.04738782, 0.97138463, 0.60490677, 0.26023875,\n",
       "       0.78101689, 0.801308  , 0.60810514, 0.91466877, 0.82750806,\n",
       "       0.89010852, 0.17091871, 0.97552324, 0.80348566, 0.88770773,\n",
       "       0.94242616, 0.83269214, 0.67716194, 0.83369571, 0.69322681,\n",
       "       0.96197398, 0.89830105, 0.60719997, 0.75138477, 0.34792153,\n",
       "       0.91974204, 0.33331784, 0.80549624, 0.3477115 , 0.83854585,\n",
       "       0.49503306, 0.32565521, 0.21366105, 0.01908444, 0.58078868,\n",
       "       0.75625353, 0.01694881, 0.04889913, 0.29944608, 0.45008794,\n",
       "       0.83696944, 0.56764219, 0.80507919, 0.87061721, 0.36590356,\n",
       "       0.92749661, 0.92417641, 0.88953585, 0.91312884, 0.69292448,\n",
       "       0.13423703, 0.81597316, 0.12964076, 0.71023051, 0.8497633 ,\n",
       "       0.8853725 , 0.9909287 , 0.8686432 , 0.79668095, 0.80822501,\n",
       "       0.84218851, 0.96321364, 0.96790301, 0.82843164, 0.29944608,\n",
       "       0.63272778, 0.85759993, 0.66957514, 0.92592308, 0.92977853,\n",
       "       0.77340614, 0.69106054, 0.90855004, 0.94981951, 0.9647343 ,\n",
       "       0.64264075, 0.97966954, 0.66291011, 0.31594999, 0.61663153,\n",
       "       0.80858622, 0.34043987, 0.84171477, 0.91528126, 0.98264637,\n",
       "       0.49849941, 0.77660933, 0.27487911, 0.73217493, 0.44323104,\n",
       "       0.42795057, 0.96534544, 0.87252266, 0.8805469 , 0.5155979 ,\n",
       "       0.88468005, 0.34993422, 0.81104144, 0.87516992, 0.66098885,\n",
       "       0.89365246, 0.17953799, 0.7461241 , 0.48116406, 0.39341065,\n",
       "       0.67915062, 0.62533331, 0.2666763 , 0.93954451, 0.53820919,\n",
       "       0.97806625, 0.67376778, 0.06244669, 0.13866463, 0.15942626,\n",
       "       0.69059333, 0.70552534, 0.94864331, 0.94270628, 0.83922329,\n",
       "       0.95373681, 0.87601677, 0.82214858, 0.92729057, 0.50967506,\n",
       "       0.83833994, 0.86145002, 0.93420597, 0.96605558, 0.93200561,\n",
       "       0.20329287, 0.19812209, 0.4937665 , 0.56598888, 0.07004922,\n",
       "       0.74020272, 0.87298808, 0.94655849, 0.92021931, 0.79752546,\n",
       "       0.82747134, 0.49572294, 0.92182209, 0.93264215, 0.96455783,\n",
       "       0.77266511, 0.87762105, 0.58078868, 0.89281304, 0.85379955,\n",
       "       0.79264028, 0.87264223, 0.38830592, 0.89703806, 0.07327098,\n",
       "       0.82665736, 0.83739453, 0.85073658, 0.93318697])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sigmoid(np.array(np.mat(xMat)*np.mat(ws)).flatten())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "predict=sigmoid(np.array(np.mat(xMat)*np.mat(ws)).flatten())\n",
    "predict=[i>0.5 for i in predict]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "Acc=np.mean(test.iloc[:,-1]==predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6588628762541806"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Acc "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def classify(x):\n",
    "    if(x>0.5):\n",
    "        return 1.0\n",
    "    return 0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def colicTest(test):\n",
    "    ws=BGD_LR(train,alpha=0.001,maxCycles=5000)\n",
    "    xMat=np.array(test.iloc[:,:-1])\n",
    "    xMat=np.column_stack((np.ones(xMat.shape[0]),xMat))\n",
    "    predict=sigmoid(np.array(np.mat(xMat)*np.mat(ws)).flatten())\n",
    "    predict=[i>0.5 for i in predict]\n",
    "    Acc=np.mean(test.iloc[:,-1]==predict)\n",
    "    return Acc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6588628762541806"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "colicTest(test)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
